"""咨询师排班模型"""
# 字段类型引入 column列
from sqlalchemy import Column, Integer, DateTime, func, ForeignKey, text
from sqlalchemy.orm import relationship

from soulapi.db import Base, engine


# 基于BaseNoel定义请求体的结构（json对象）
class TeaSchedule(Base):
    __tablename__ = 'teaschedule'  # 数据表表名，__tablename__：设置映射到的表名不设置，默认映射到类名post下
    __table_args__ = {'comment': '咨询师排班（某时间段的状态）'}
    ts_id = Column(Integer, primary_key=True, index=True, autoincrement=True, server_default=text("0"), comment='排班ID')  # index索引设置,autoincremen自增长
    flag = Column(Integer,  nullable=False, server_default=text("1"), comment="0=繁忙,1=空闲")
    # 外键
    tea_id = Column(Integer, ForeignKey('teachers.tea_id'), comment="咨询师")
    tt_id = Column(Integer, ForeignKey('teatime.tt_id'), comment="时间表")
    # 关系
    teachers = relationship('Teachers', back_populates='teaschedule', foreign_keys=[tea_id])
    teatime = relationship('TeaTime', back_populates='teaschedule', foreign_keys=[tt_id])
    orderinfo = relationship('OrderInfo', back_populates='teaschedule')
    # 时间获取TeaTime
    created_at = Column(DateTime, server_default=func.now(), comment='创建时间')
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment='更新时间')


# 在数据库中生成表
Base.metadata.create_all(engine)